//
//  FourViewModel.m
//  DemoTest
//
//  Created by DaShuEdu on 2017/5/4.
//  Copyright © 2017年 DaShuEdu. All rights reserved.
//

#import "FourViewModel.h"
#import "FOneCell.h"
#import "FTwoCell.h"
#import "FThreeCell.h"
#import "InfoCellModel.h"
#import "FFourCell.h"

@interface FourViewModel () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *items;

@end

@implementation FourViewModel

- (id)initWithItems:(NSArray *)items
     didSelectBlock:(DidSelectCellBlock)didSelectBlock {
    if (self = [super init]) {
        self.items = items;
        self.didSelectCellBlock = didSelectBlock;
    }
    return self;
}

- (id)itemAtIndexPath:(NSIndexPath *)indexPath {
    return self.items[indexPath.section][indexPath.row];
}

- (void)handleTableViewDataSourceAndDelegate:(UITableView *)tableView {
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FOneCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([FOneCell class])];
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FTwoCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([FTwoCell class])];
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FThreeCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([FThreeCell class])];
    [tableView registerClass:[FFourCell class] forCellReuseIdentifier:NSStringFromClass([FFourCell class])];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.items[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InfoCellModel *model = self.items[indexPath.section][indexPath.row];
    if (model.content.length >= 1) {
        FTwoCell *cell = (FTwoCell *)[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FTwoCell class])];
        [cell cellWithModel:model];
        return cell;
    } else {
        if (indexPath.row < 1) {
            FOneCell *cell = (FOneCell *)[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FOneCell class])];
            [cell cellWithModel:model];
            return cell;
        } else if (indexPath.row < 2) {
            FThreeCell *cell = (FThreeCell *)[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FThreeCell class])];
            [cell cellWithModel:model];
            return cell;
        } else {
            FFourCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FFourCell class])];
//            if (cell==nil) {
//                NSLog(@"hahahahaha");
//                cell = [[FFourCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([FFourCell class])];
//            }
            [cell cellWithModel:model];
            return cell;
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.items count];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    id item = [self itemAtIndexPath:indexPath];
    if (self.didSelectCellBlock) {
        self.didSelectCellBlock(indexPath, item);
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    InfoCellModel *model = self.items[indexPath.section][indexPath.row];
    if (model.height>0) {
        return;
    }
    model.height = cell.frame.size.height;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath  {
    InfoCellModel *model = self.items[indexPath.section][indexPath.row];
    if (model.height>0) {
        return model.height;
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 50;
}

@end
